<?php
// @formatter:off
/**
 * @file WCErrors.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCErrors.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/WCErrors.php $
 */
// @formatter:on

class WCErrors extends WCSingleton {
	public function error($code, $message) {
		$message = $this->generateInfo(debug_backtrace(), $code, $message);

		global $wcDefaults;

		if($wcDefaults["errors"]["errors"]) {
			echo "\n{$message}\n\n";
			debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
			if($wcDefaults["errors"]["logs"]) {
				//
				// Loading global pointers.
				global $wcLog;
				//
				// Informing error on logs.
				if($wcLog) {
					$wcLog->write($message);
					///@todo check level;
				}
			}
			if($wcDefaults["errors"]["dies"]) {
				die ;
			}
		}
	}
	public function warning($code, $message) {
		global $wcDefaults;

		if($wcDefaults["errors"]["warnings"]) {
			$this->setGlobals($code, $message);
		}

		$message = $this->generateInfo(debug_backtrace(), $code, $message);

		global $wcDefaults;

		if($wcDefaults["errors"]["warnings"]) {
			echo "\n{$message}\n\n";
			debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
			if($wcDefaults["errors"]["logs"]) {
				//
				// Loading global pointers.
				global $wcLog;
				//
				// Informing error on logs.
				if($wcLog) {
					$wcLog->write($message);
					///@todo check level;
				}
			}
			echo "\n";
		}
	}
	//
	// Protected methods.
	protected function generateInfo($bt, $code, $message) {
		$btCaller = array_shift($bt);
		$file = $btCaller["file"];
		$line = $btCaller["line"];

		$btCaller = array_shift($bt);
		$caller = "{$btCaller["class"]}{$btCaller["type"]}{$btCaller["function"]}()";

		return "{$caller}: [{$code}] {$message} ({$file}:{$line})";
	}
	protected function setGlobals($code, $message) {
		global $wcErrno;
		global $wcError;

		$wcErrno = $code;
		$wcError = $message;
	}
}
